package com.dubbo.exceptions;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liufl
 * @version 创建时间：2017年7月25日 下午5:58:43
 * 类说明
 */
public class RPCExcepetion implements ExceptionMapper<Exception> {
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	/**
     * 异常处理
     * 
     * @param exception
     * @return 异常处理后的Response对象
     */
    @Override
    public Response toResponse(Exception exception){
        String response;
        int code;
        if(exception instanceof MyException){
            MyException myException=(MyException)exception;
            response="{\"resp_code\":\""+myException.getErrorCode()+"\",\"resp_info\":\""+myException.getErrorMessage()+"\"}";
            code=0;
        }else{
            response="请求失败";
            log.error(exception.getMessage(), exception);
            code=-99;
        }
        return Response.ok(response, MediaType.APPLICATION_JSON).status(code)
                .build();
    }
}

